﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using RentIt06DummyClient.CreateAccountSession;

namespace RentIt06DummyClient
{
    using System.IO;

    using System.Security.Cryptography;

    using RentIt06DummyClient.AdminSession;
    using RentIt06DummyClient.CreateAccountSession;
    using RentIt06DummyClient.FileService;
    using RentIt06DummyClient.UserSession;
    using RentIt06DummyClient.PublisherSession;
    using RentIt06DummyClient.FacebookLoginService;

    using MediaType = RentIt06DummyClient.PublisherSession.MediaType;

    class Program
    {
        private static int mID;
        static void Main(string[] args)
        {

            //createAccount();

            //UploadFile();

            //DownloadFile();

            //facebook();

            //test();

            Console.Write("Done..");
            Console.ReadLine();
        }

        static void test()
        {
            //Får account already exists ved lang mail no prob med password.
            var user = new AdminSessionClient();
            //acc.CreateAccount("deepa@gmail.com", "deepa");
            user.Login("jannek", "qwerty");
            //Console.WriteLine(user.ReadAccountId(17));
            Console.WriteLine(user.ReadAccountEmail("deepa@gmail.com"));
            //user.ChangeAccountAccessLevel(i, PermissionGroup.Admin);
            user.Logout();
            user.Close();
        }

        static void DownloadFile()
        {
            var user = new UserSessionClient();
            user.Login("jannek", "qwerty");
            int token = user.GetDownloadToken(mID);
            var file = new FileServiceClient();
            Console.WriteLine("DL starting of ID:" + mID + " token:" + token);
            var dims = file.DownloadMedia(token, mID);
            Console.WriteLine("DL2");

            var shizzle = new FileStream(@"C:\Users\DCoin\Desktop\tester.jpg", FileMode.Create);
            const int bufferLen = 65000;
            byte[] buffer = new byte[bufferLen];
            int count = 0;
            while ((count = dims.Read(buffer, 0, bufferLen)) > 0)
            {
                shizzle.Write(buffer, 0, count);
            }
        }

        static void createAccount()
        {
            var ds = new CreateAccountSessionClient();
            ds.CreateAccount("jegk", "qwerty");
            ds.Close();
        }

        static void UploadFile() {
            var path3p9mb = @"C:\Users\Film\seeds\Assets\Textures\NGJ_bg01.png";
            var path0p9mb = @"C:\Users\Film\Pictures\trysil\1123.jpg";
            var path0p1gb = @"C:\Users\Film\Downloads\java_ee_sdk-6u4-jdk-windows-x64.exe";
            var path0p5gb = @"C:\Users\Film\Downloads\UnitySetup-3.5.0b6.exe";
            var path02mb = @"C:\Users\DCoin\Pictures\23kb.jpg";
            var path4mb = @"C:\Users\DCoin\Pictures\4mb.jpg";
            var path68mb = @"C:\Users\DCoin\Downloads\iTunes64Setup.exe";
            var path412mb = @"C:\Users\DCoin\Downloads\UnitySetup-3.4.2.exe";

            var filePath = path4mb;

            var fs = new FileServiceClient();
            Stream s = new FileStream(filePath, FileMode.Open);

            SHA512 sha = SHA512.Create();

            byte[] hash412mbfile = { 0x92, 0x46, 0xAC, 0x5B, 0xE6, 0x31, 0xC7, 0x94, 0xB0, 0xC4, 0xEF, 0x1B, 0x0C, 0x95, 0xDA, 0xA9, 0x33, 0x18, 0x29, 0xF8, 0x0C, 0x69, 0x35, 0x9E, 0x8E, 0xA8, 0x5C, 0xBB, 0x42, 0xF9, 0x65, 0x88, 0xDF, 0x19, 0x5A, 0x64, 0x87, 0xB3, 0x24, 0x3D, 0x09, 0x69, 0x8C, 0xAC, 0x4B, 0x47, 0x10, 0x19, 0xAA, 0x02, 0x11, 0x65, 0x14, 0x6D, 0xFD, 0x0E, 0x4F, 0x67, 0x61, 0x76, 0x6B, 0x48, 0x31, 0xEA };

            byte[] hash = hash412mbfile;
            hash = sha.ComputeHash(s);
            s.Close();
            Console.WriteLine(BitConverter.ToString(hash).Replace("-",",0x"));
            var ps = new PublisherSessionClient();

            var acc = ps.Login("jannek", "qwerty");

            Console.WriteLine("Logged in with " + acc.Id);

            mID = ps.CreateMedia();

            Console.WriteLine("Media: " + mID);

            int token = ps.GetUploadToken(mID, hash);

            Console.WriteLine("Token: " + token);

            Console.WriteLine(BitConverter.ToString(hash).Replace("-", ""));

            s = new FileStream(filePath, FileMode.Open);

            fs.UploadMedia(token,s);
            /*
            var med = ps.ReadMedia(mID);
            med.CanBeBought = true;
            med.CanBeRented = true;
            med.PurchasePrice = 0;
            med.RentingPrice = 0;
            med.Filename = "Test.jpg";
            med.MediaType = MediaType.Image;
            med.Author = "Johny Reimer";
            med.Title = "The Pic";
            ps.UpdateMedia(med);
            ps.PublishMedia(mID);
            var us = new UserSessionClient();
            us.Login("jannek", "qwerty");
            us.RentProduct(mID);*/
            s.Close();
        }

        static void facebook()
        {
            //http://csharpsdk.org/docs/web/getting-started
            var fb = new FacebookLoginServiceClient();
            FacebookAuth auth = fb.GetAuth();

            Console.WriteLine(auth.UserId);
            Console.WriteLine(auth.AccessToken);
            Console.WriteLine(auth.SessionKey);
        }
    }
}
